package com.base.utils;

import com.base.comm.CommUrls;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

@Component
public class SyntheticPicUtils {
	//读取配置文件
//	@Value("${system.file-path}")
//	private String filePath;
	/**
     * 合成图片
     * @param tdcfile   二维码路径
     * @param sharepicfile   后台上传图片路径
     * @param picid   后台上传图片id
     * @param userid   用户id
     * @param x   二维码左边距
     * @param y   二维码上边距
     * @return      返回合成图片路径
     * @throws Exception
     */
	public static String insertImage(String tdcfile, String sharepicfile,String picid,String userid,
										Integer x, Integer y) throws Exception {
		File file1= new File(tdcfile);
//		File file1= new File("D:/Files/kongbai.png");
		File file2= new File(sharepicfile);

		System.out.println(sharepicfile);

		BufferedImage image= ImageIO.read(file1);

		BufferedImage bg= ImageIO.read(file2);

		Graphics2D g=bg.createGraphics();

		//file2 要比file1尺寸大 否则会被覆盖
		int width=image.getWidth(null) > bg.getWidth() * 5/10? (bg.getWidth() * 5/10) : image.getWidth(null);

		int height=image.getHeight(null) > bg.getHeight() *5 /10? (bg.getHeight() * 5/10) : image.getWidth(null);

		//设置图片
		g.drawImage(image,x,y,width,height,null);

		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,RenderingHints.VALUE_STROKE_DEFAULT);

		g.dispose();

		bg.flush();

		image.flush();

		//TODO 本地测试路径
//		String synthetic = "D:/Files/"+picid+userid+".png";
		//TODO 服务器路径
		String synthetic = CommUrls.FILEPATH+"Synthetic/"+picid+userid+".png";

		OutputStream ops = new FileOutputStream(new File(synthetic));

		ImageIO.write(bg,"png", ops);

		return synthetic;

	}

	public static String insertImage2(String qrcodefile, String gbpicfile, String mobile, Integer x, Integer y) throws Exception {
		File file1= new File(qrcodefile);
		File file2= new File(gbpicfile);

		BufferedImage image= ImageIO.read(file1);

		BufferedImage bg= ImageIO.read(file2);

		Graphics2D g=bg.createGraphics();

		//file2 要比file1尺寸大 否则会被覆盖
		int width=image.getWidth(null) > bg.getWidth() * 5/10? (bg.getWidth() * 5/10) : image.getWidth(null);

		int height=image.getHeight(null) > bg.getHeight() *5 /10? (bg.getHeight() * 5/10) : image.getWidth(null);

		//设置图片
		g.drawImage(image,x,y,width,height,null);

		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,RenderingHints.VALUE_STROKE_DEFAULT);

		g.dispose();

		bg.flush();

		image.flush();

		// 合成图片文件
		String filename = mobile + ".png";
		// 合成图片文件夹
		String fileReal = CommUrls.FILEPATH + "/" + new SimpleDateFormat("yyyy-MM-dd").format(Produce.getNowDate()) + "Synthetic";
		File filepath = new File(fileReal, filename);
		if (!filepath.getParentFile().exists()) {
			filepath.getParentFile().mkdirs();
		}

		OutputStream ops = new FileOutputStream(filepath);

		ImageIO.write(bg,"png", ops);

		return fileReal + "/" + filename;

	}
	
	//改变二维码大小
	public static void zoomImage(String src, String userid, int toWidth, int toHeight) throws IOException {
		
		BufferedImage result = null;
 
		try {
			File srcfile = new File(src);
			if (!srcfile.exists()) {
				System.out.println("文件不存在");
				
			}
			BufferedImage im = ImageIO.read(srcfile);
			
			/* 新生成结果图片 */
			result = new BufferedImage(toWidth, toHeight,
					BufferedImage.TYPE_INT_RGB);
			
			Graphics2D g = result.createGraphics();//获得一个Graphics2D对象
			
			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
			
			g.drawImage(im.getScaledInstance(toWidth, toHeight,
							java.awt.Image.SCALE_SMOOTH), 0, 0, null);
			
 
		} catch (Exception e) {
			System.out.println("创建缩略图发生异常" + e.getMessage());
		}
		
//		String synthetic = "D:/Files/"+userid+".png";
		//TODO 服务器路径
		String synthetic = CommUrls.FILEPATH+"QRCode/"+userid+".png";
		
		OutputStream ops = new FileOutputStream(new File(synthetic));
		
		ImageIO.write(result ,"png", ops);
 
	}
	
	//缩小合成的图片
    public static String smallImage(HttpServletRequest request, String src, String userid, String picid) throws IOException {
		
		BufferedImage result = null;
 
		try {
			File srcfile = new File(src);
			if (!srcfile.exists()) {
				System.out.println("文件不存在");
				
			}
			BufferedImage im = ImageIO.read(srcfile);
			
			/* 新生成结果图片 */
			result = new BufferedImage(265, 472,
					BufferedImage.TYPE_INT_RGB);
			
			Graphics2D g = result.createGraphics();//获得一个Graphics2D对象
			
			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
			
			g.drawImage(im.getScaledInstance(265, 472,
							java.awt.Image.SCALE_SMOOTH), 0, 0, null);
			
 
		} catch (Exception e) {
			System.out.println("创建缩略图发生异常" + e.getMessage());
		}
		
//		String synthetic = "D:/Files/"+picid+userid+".png";
		//TODO 服务器路径
		String synthetic = CommUrls.FILEPATH+"Synthetic/"+picid+userid+".png";
		
		OutputStream ops = new FileOutputStream(new File(synthetic));
		
		ImageIO.write(result ,"png", ops);
		
		//TODO 服务器图片访问路径
		// 获取域名
		String basePath1 = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();

		String path = basePath1 + "/file/Synthetic/"+picid+userid+".png";
		
		return path;
 
	}

}
